<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:dg="livcos.org/ns/doc/diagram"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns:f="livcos.org/ns/xslt/function"
	exclude-result-prefixes="dg f xs">

	<xsl:import href="base.xsl"/>
	
	<xsl:template match="dg:*" mode="extend">
		<xsl:sequence select="."/>
	</xsl:template>
	
	<xsl:template match="dg:box" mode="extend">
		<xsl:copy>
			<xsl:sequence select="@*"/>
			<xsl:variable name="w" select="if (@width) then @width else string-length(@id) * 10 + 20"/>
			<xsl:variable name="h" select="if (@height) then @height else 50"/>
			<xsl:variable name="a" select="if (@angle) then @angle else 0"/>
			<xsl:variable name="c" select="f:castV(@pos)"/>
			<xsl:variable name="m" select="f:rotateV(($w div 2, $h div 2), $a)"/>
			<xsl:variable name="viewPortMaxV" select="f:addV($c, $m)"/>
			<xsl:attribute name="viewPortMaxX">
				<xsl:value-of select="$viewPortMaxV[1]"/>
			</xsl:attribute>
			<xsl:attribute name="viewPortMaxY">
				<xsl:value-of select="$viewPortMaxV[2]"/>
			</xsl:attribute>
			<xsl:variable name="viewPortMinV" select="f:subV($c, $m)"/>
			<xsl:attribute name="viewPortMinX">
				<xsl:value-of select="$viewPortMinV[1]"/>
			</xsl:attribute>
			<xsl:attribute name="viewPortMinY">
				<xsl:value-of select="$viewPortMinV[2]"/>
			</xsl:attribute>
			<xsl:attribute name="width">
				<xsl:value-of select="$w"/>
			</xsl:attribute>
			<xsl:attribute name="height">
				<xsl:value-of select="$h"/>
			</xsl:attribute>
			<xsl:attribute name="angle">
				<xsl:value-of select="$a"/>
			</xsl:attribute>
			<xsl:sequence select="node()"/>
			<xsl:if test="not(node())"><xsl:value-of select="@id"/></xsl:if>
		</xsl:copy>
	</xsl:template>
	
	<xsl:template match="dg:circle" mode="extend">
		<xsl:copy>
			<xsl:sequence select="@*"/>
			<xsl:variable name="r" select="if (@radius) then @radius else string-length(@id) * 5 + 6"/>
			<xsl:variable name="c" select="f:castV(@pos)"/>
			<xsl:variable name="m" select="$r, $r"/>
			<xsl:variable name="viewPortMaxV" select="f:addV($c, $m)"/>
			<xsl:attribute name="viewPortMaxX">
				<xsl:value-of select="$viewPortMaxV[1]"/>
			</xsl:attribute>
			<xsl:attribute name="viewPortMaxY">
				<xsl:value-of select="$viewPortMaxV[2]"/>
			</xsl:attribute>
			<xsl:variable name="viewPortMinV" select="f:subV($c, $m)"/>
			<xsl:attribute name="viewPortMinX">
				<xsl:value-of select="$viewPortMinV[1]"/>
			</xsl:attribute>
			<xsl:attribute name="viewPortMinY">
				<xsl:value-of select="$viewPortMinV[2]"/>
			</xsl:attribute>
			<xsl:attribute name="radius">
				<xsl:value-of select="$r"/>
			</xsl:attribute>
			<xsl:sequence select="node()"/>
			<xsl:if test="not(node())"><xsl:value-of select="@id"/></xsl:if>
		</xsl:copy>
	</xsl:template>
	
	<xsl:template match="dg:connect" mode="extend">
		<xsl:copy>
			<xsl:sequence select="@*"/>
			<xsl:variable name="path" select="../dg:path[@ref = current()/@ref]"/>
			<xsl:if test="$path">
				<xsl:attribute name="id" select="$path/@id"/>
				<xsl:sequence select="$path/dg:*"/>
			</xsl:if>
			<xsl:sequence select="*"/>
		</xsl:copy>
	</xsl:template>
	
	<xsl:template match="dg:connect[@end-ref]" mode="extend">
		<xsl:variable name="eB" select="../dg:*[@ref = current()/@end-ref]"/>
		<xsl:if test="$eB">
			<xsl:copy>
				<xsl:sequence select="@*"/>
				<xsl:attribute name="end" select="$eB/@id"/>
				<xsl:variable name="path" select="../dg:path[@ref = current()/@ref]"/>
				<xsl:if test="$path">
					<xsl:attribute name="id" select="$path/@id"/>
					<xsl:sequence select="$path/dg:*"/>
				</xsl:if>
				<xsl:sequence select="*"/>
			</xsl:copy>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="dg:path" mode="extend">
		<xsl:copy>
			<xsl:sequence select="@*"/>
			<xsl:variable name="coords">
				<xsl:for-each select="tokenize(string-join(@path, ' '), ' ')">
					<xsl:variable name="c" select="tokenize(., ',')"/>
					<c x="{$c[1]}" y="{$c[2]}"/>
				</xsl:for-each>
				<xsl:for-each select="dg:*/@to">
					<xsl:variable name="c" select="tokenize(., ',')"/>
					<c x="{$c[1]}" y="{$c[2]}"/>
				</xsl:for-each>
			</xsl:variable>
			<xsl:if test="$coords/*">
				<xsl:attribute name="viewPortMaxX">
					<xsl:value-of select="max($coords/*/@x)"/>
				</xsl:attribute>
				<xsl:attribute name="viewPortMaxY">
					<xsl:value-of select="max($coords/*/@y)"/>
				</xsl:attribute>
				<xsl:attribute name="viewPortMinX">
					<xsl:value-of select="min($coords/*/@x)"/>
				</xsl:attribute>
				<xsl:attribute name="viewPortMinY">
					<xsl:value-of select="min($coords/*/@y)"/>
				</xsl:attribute>
			</xsl:if>
		</xsl:copy>
	</xsl:template>
	
</xsl:stylesheet>